package com.blt.car.journey;

import android.widget.ListView;

import com.blt.car.R;
import com.blt.car.base.BaseFragment;
import com.blt.car.base.ViewHolder;
import com.blt.car.journey.adapter.JourHomeAdapter;
import com.chanven.lib.cptr.PtrClassicFrameLayout;
import com.chanven.lib.cptr.PtrDefaultHandler;
import com.chanven.lib.cptr.PtrFrameLayout;
import com.chanven.lib.cptr.loadmore.OnLoadMoreListener;


import blt.app_core.api.base.CoreEvent;
import blt.app_core.api.base.CoreManager;
import blt.app_core.api.journey.JourneyApi;
import blt.app_core.api.journey.bean.Travel;
import blt.app_core.log.Logger;

/**
 * Created by xgp on 2017/6/13.
 */

public class JourneyFragment extends BaseFragment {
    PtrClassicFrameLayout ptrClassicFrameLayout;

    private ListView mListview;
    private JourHomeAdapter jourHomeAdapter;
    //    private TextView totalPath;
//    private TextView totalAccount;
    private int page = 1;
    private int size = 10;


    @Override
    protected int getLayoutId() {
        return R.layout.fragment_journey;
    }

    @Override
    protected void initData() {
        super.initData();
        CoreManager.getCore(JourneyApi.class).getTravel(page, size, false, false);
    }

    @Override
    protected void initView(ViewHolder holder) {
//        totalPath = holder.get(R.id.journey_path);
//        totalAccount = holder.get(R.id.journey_total_account);
        mListview = holder.get(R.id.journey_home_listview);
        jourHomeAdapter = new JourHomeAdapter(getActivity());
        mListview.setAdapter(jourHomeAdapter);
        ptrClassicFrameLayout = holder.get(R.id.journey_frame);
        ptrClassicFrameLayout.setLoadMoreEnable(true);
        ptrClassicFrameLayout.setPtrHandler(new PtrDefaultHandler() {
            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                page = 1;
                CoreManager.getCore(JourneyApi.class).getTravel(page, size, false, false);
            }
        });
        ptrClassicFrameLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void loadMore() {
                page++;
                Logger.d("page" + page);
                CoreManager.getCore(JourneyApi.class).getTravel(page, size, false, false);
            }
        });

    }


    @CoreEvent(coreClientClass = JourneyApi.class)
    public void onTravel(Travel travel) {
        if (page == 1) {
            jourHomeAdapter.clear();
            ptrClassicFrameLayout.refreshComplete();
        } else {
            ptrClassicFrameLayout.loadMoreComplete(true);
        }
//        totalPath.setText(getResources().getString(R.string.total_distance, travel.totalDistance));
//        totalAccount.setText(getResources().getString(R.string.total_account, travel.totalAmount));
        jourHomeAdapter.add(travel.travelOrderList);
    }
}
